package com.main.by.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.main.by.dao.GeneralDao;
import com.main.by.dao.UserDao;
import com.main.by.entity.bo.UserLoginBO;
import com.main.by.entity.vo.NewsInfoVO;
import com.main.by.entity.vo.UserRegisterVO;
import com.main.by.util.StringUtil;


@Repository
public class UserDaoImpl implements UserDao {
Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralDao generalDao;

	@Override
	public UserLoginBO getUserByUserName(String userName) throws Exception {
		if(StringUtil.isEmpty(userName)){
			return null;
		}
		return generalDao.getEntity(UserLoginBO.class, " select * from renren_user where userName = ? ", new Object[]{userName});
	}

	@Override
	public boolean updateSysUser(UserLoginBO user, String... fileds)
			throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
	
	public boolean insterRegistInfo(UserRegisterVO user) throws Exception{
		boolean successful = generalDao.saveEntity("insert into renren_user(userName,passWord,flag) values(?,MD5(?),?)", 
				new Object[]{user.getUserName(),user.getPassWord(),0});
		return successful;
	}
	
	@Override
	public List<UserLoginBO> findUser() throws Exception {
		return (List<UserLoginBO>) generalDao.findAllEntity(UserLoginBO.class, "select * from renren_user where flag = 1");
	}
	
	@Override
	public List<UserLoginBO> findPendingUser() throws Exception {
		return (List<UserLoginBO>) generalDao.findAllEntity(UserLoginBO.class, "select * from renren_user where flag = 0");
	}
	
	@Override
	public boolean auditUserInfo(int userId) throws Exception {
		return generalDao.updateEntity("update renren_user set flag=1 where userId=? ",new Object[]{userId});
	}
	
	@Override
	public boolean deleteUserById(int userId) throws Exception {
		return generalDao.updateEntity("delete from renren_user where userId=? ",new Object[]{userId});
	}
}
